import { getCurrentInstance, onMounted, reactive } from 'vue';
import { api } from '@/app/shop/admin/data/data.service';

export default function useFakeUser() {
  const { proxy } = getCurrentInstance();

  const fakeUser = reactive({ nickname: '', id: '' });

  const deliverCompany = reactive({
    loading: false,
    data: [],
    pageData: {
      page: 1,
      list_rows: 10,
      total: 0,
    },
  });

  async function getDeliverCompany(keyword) {
    let search = {};
    if (keyword) {
      search = { keyword: keyword };
    }
    const { data } = await api.fakeUser.select({
      page: deliverCompany.pageData.page,
      list_rows: deliverCompany.pageData.list_rows,
      search: JSON.stringify(search),
    });
    deliverCompany.data = data.data;
    deliverCompany.pageData.page = data.current_page;
    deliverCompany.pageData.list_rows = data.per_page;
    deliverCompany.pageData.total = data.total;
  }

  function onChangeExpressCode(code) {
    fakeUser.nickname = proxy.$refs[`dc-${code}`][0].label;
  }

  function remoteMethod(keyword) {
    deliverCompany.loading = true;
    setTimeout(() => {
      deliverCompany.loading = false;
      getDeliverCompany(keyword);
    }, 200);
  }

  onMounted(() => {
    getDeliverCompany();
  });
  return {
    fakeUser,
    deliverCompany,
    getDeliverCompany,
    onChangeExpressCode,
    remoteMethod,
  };
}
